/*
 * =============================================================================
 * 
 *   Copyright (c) 2011, The THYMELEAF team (http://www.thymeleaf.org)
 * 
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 * 
 *       http://www.apache.org/licenses/LICENSE-2.0
 * 
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 * 
 * =============================================================================
 */
package thymeleafexamples.stsm.web.controller;

import java.util.*; 

import javax.servlet.http.*;

import org.springframework.beans.factory.annotation.*;  
import org.springframework.stereotype.*;
import org.springframework.ui.*;
import org.springframework.validation.*; 
import org.springframework.web.bind.annotation.*; 
import thymeleafexamples.stsm.business.entities.*; 
import thymeleafexamples.stsm.business.services.*;

@Controller
public class SeedStarterMngController { 

	@Autowired
	private VarietyService varietyService;

	@Autowired
	private SeedStarterService seedStarterService;

	public SeedStarterMngController() {
		super();
	}

	@ModelAttribute("allTypes")
	public List<Type> populateTypes() {
		return Arrays.asList(Type.ALL);
	}

	@ModelAttribute("allFeatures")
	public List<Feature> populateFeatures() {
		return Arrays.asList(Feature.ALL);
	}

	@ModelAttribute("allVarieties")
	public List<Variety> populateVarieties() {
		return this.varietyService.findAll();
	}

	@ModelAttribute("allSeedStarters")
	public List<SeedStarter> populateSeedStarters() {
		return this.seedStarterService.findAll();
	}

	@RequestMapping({ "/seedstartermng.con" })
	public String showSeedstarters(final SeedStarter seedStarter, final ModelMap model ) {
		seedStarter.setDatePlanted(Calendar.getInstance().getTime());
		model.addAttribute( "myAttr", "안녕하세요!" );
		return "seedstartermng.html";
	}

	@RequestMapping(value = "/seedstartermng.con", params = { "save" })
	public String saveSeedstarter(final SeedStarter seedStarter, final BindingResult bindingResult, final ModelMap model) {
		if (bindingResult.hasErrors()) {
			return "seedstartermng.html";
		}
		this.seedStarterService.add(seedStarter);
		model.clear();
		return "redirect:/seedstartermng.con";
	}

	@RequestMapping(value = "/seedstartermng.con", params = { "addRow" })
	public String addRow(final SeedStarter seedStarter, final BindingResult bindingResult) {
		seedStarter.getRows().add(new Row());
		return "seedstartermng.html";
	}

	@RequestMapping(value = "/seedstartermng.con", params = { "removeRow" })
	public String removeRow(final SeedStarter seedStarter, final BindingResult bindingResult, final HttpServletRequest req) {
		final Integer rowId = Integer.valueOf(req.getParameter("removeRow"));
		seedStarter.getRows().remove(rowId.intValue());
		return "seedstartermng.html";
	}

}
